package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.PipConfig;
import cn.stylefeng.guns.modular.gridsystem.mapper.PipConfigMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.PipConfigParam;
import cn.stylefeng.guns.modular.gridsystem.service.PipConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 管道配置 服务实现类
 *
 * @author ssy
 * @date 2021/11/22 11:57
 */
@Service
public class PipConfigServiceImpl extends ServiceImpl<PipConfigMapper, PipConfig> implements PipConfigService {

    @Override
    public PageResult<PipConfig> page(PipConfigParam pipConfigParam) {

        // 构造条件
        LambdaQueryWrapper<PipConfig> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<PipConfig> list(PipConfigParam pipConfigParam) {

        // 构造条件
        LambdaQueryWrapper<PipConfig> queryWrapper = new LambdaQueryWrapper<>();


        return this.list(queryWrapper);
    }

    @Override
    public void add(PipConfigParam pipConfigParam) {

        // 校验参数，检查是否存在重复的编码或者名称，不排除当前添加的这条记录
        checkParam(pipConfigParam, false);

        // 将dto转为实体
        PipConfig pipConfig = new PipConfig();
        BeanUtil.copyProperties(pipConfigParam, pipConfig);


        this.save(pipConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(PipConfigParam pipConfigParam) {

        // 根据id查询实体
        PipConfig pipConfig = this.queryPipConfig(pipConfigParam);

        this.removeById(pipConfig.getId());

    }

    @Override
    public void edit(PipConfigParam pipConfigParam) {

        // 根据id查询实体
        PipConfig pipConfig = this.queryPipConfig(pipConfigParam);

        // 校验参数，检查是否存在重复的编码或者名称，排除当前编辑的这条记录
        checkParam(pipConfigParam, true);

        // 请求参数转化为实体
        BeanUtil.copyProperties(pipConfigParam, pipConfig);

        this.updateById(pipConfig);
    }

    @Override
    public PipConfig detail(PipConfigParam pipConfigParam) {
        return this.queryPipConfig(pipConfigParam);
    }

    @Override
    public void changeStatus(PipConfigParam pipConfigParam) {
        Long id = pipConfigParam.getId();
        Integer status = pipConfigParam.getStatus();


        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<PipConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .eq(PipConfig::getId, id)
            .set(PipConfig::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }

    @Override
    public int countByCode(String code) {
        QueryWrapper<PipConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code",code);
        return this.count(queryWrapper);
    }

    @Override
    public String detailByCode(String code) {
        QueryWrapper<PipConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code",code);
        PipConfig one = this.getOne(queryWrapper);
        String value = null;
        if(ObjectUtil.isNotNull(one)){
            value = one.getValue();
        }
        return value;
    }

    /**
     * 校验参数，检查是否存在重复的编码等
     *
     * @author ssy
     * @since 2021/11/22 11:57
     */
    private void checkParam(PipConfigParam pipConfigParam, boolean isExcludeSelf) {
        Long id = pipConfigParam.getId();
        String code = pipConfigParam.getCode();

        // 构建带code的查询条件
        LambdaQueryWrapper<PipConfig> queryWrapperByCode = new LambdaQueryWrapper<>();
        queryWrapperByCode.eq(PipConfig::getCode, code);

        // 如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            queryWrapperByCode.ne(PipConfig::getId, id);
        }


    }

    /**
     * 获取管道配置
     *
     * @author ssy
     * @since 2021/11/22 11:57
     */
    private PipConfig queryPipConfig(PipConfigParam pipConfigParam) {
        PipConfig pipConfig = this.getById(pipConfigParam.getId());
        return pipConfig;
    }
}
